package com.athuahua.webflux.exception;

import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: huahua
 * @Date: 2024/11/18 0018
 * @Time: 16:49
 * @Description:
 */
//@ResponseBody
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionHandler {

//    @ExceptionHandler(ArithmeticException.class)
//    public String error(ArithmeticException e) {
//        System.out.println("发生了数学运算异常" + e);
//        return "炸了....哈哈哈";
//
//    }

    //新版本错误处理返回值多了 ProblemDetail  ErrorResponse
    @ExceptionHandler(ArithmeticException.class)
    public ErrorResponse error(ArithmeticException e) {
        System.out.println("发生了数学运算异常" + e);


        //return ProblemDetail.forStatus(203);
        ErrorResponse build = ErrorResponse.builder(
                e, ProblemDetail.forStatus(203)
        ).build();
        return build;
    }

}
